package com.ruoyi.module.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.module.domain.Order;
import com.ruoyi.module.domain.OrderGoods;
import com.ruoyi.module.service.IOrderService;
import com.ruoyi.module.vo.EvaluationVo;
import com.ruoyi.system.service.ISysUserService;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.module.mapper.EvaluateMapper;
import com.ruoyi.module.domain.Evaluate;
import com.ruoyi.module.service.IEvaluateService;

/**
 * 用户评论Service业务层处理
 *
 * @author fubojin
 * @date 2024-12-16
 */
@Service
public class EvaluateServiceImpl implements IEvaluateService
{
    @Autowired
    private EvaluateMapper evaluateMapper;

    @Autowired
    private IOrderService orderService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询用户评论
     *
     * @param id 用户评论主键
     * @return 用户评论
     */
    @Override
    public Evaluate selectEvaluateById(Long id)
    {
        return evaluateMapper.selectEvaluateById(id);
    }

    /**
     * 查询用户评论列表
     *
     * @param evaluate 用户评论
     * @return 用户评论
     */
    @Override
    public List<Evaluate> selectEvaluateList(Evaluate evaluate)
    {
        return evaluateMapper.selectEvaluateList(evaluate);
    }

    /**
     * 查询评论商品数量
     *
     * @param goodsId 商品ID
     * @return 数量
     */
    @Override
    public Integer selectEvaluateSum(Long goodsId)
    {
        Evaluate evaluate = new Evaluate();
        evaluate.setGoodsId(goodsId);
        List<Evaluate> evaluates = evaluateMapper.selectEvaluateList(evaluate);
        if (evaluates==null){
            return 0;
        }
        return evaluates.size();
    }

    /**
     * 新增用户评论
     *
     * @param evaluate 用户评论
     * @return 结果
     */
    @Override
    public int insertEvaluate(Evaluate evaluate)
    {
        evaluate.setCreateTime(DateUtils.getNowDate());

        if (evaluate.getType()!=null && evaluate.getType()==2){
            //订单
            Order order = orderService.selectOrderById(evaluate.getOrderId());
            List<OrderGoods> orderGoodsList = order.getOrderGoodsList();
            for (OrderGoods orderGoods : orderGoodsList) {
                evaluate.setGoodsId(orderGoods.getGoodsId());
                evaluate.setGoodsName(orderGoods.getGoodsName());
                evaluateMapper.insertEvaluate(evaluate);
                evaluate.setId(null);
            }
            orderService.updateOrderEvaluate(evaluate.getOrderId());//改变订单评论状态
        }else {
            evaluateMapper.insertEvaluate(evaluate);
        }

        return 1;
    }

    /**
     * 修改用户评论
     *
     * @param evaluate 用户评论
     * @return 结果
     */
    @Override
    public int updateEvaluate(Evaluate evaluate)
    {
        evaluate.setUpdateTime(DateUtils.getNowDate());
        return evaluateMapper.updateEvaluate(evaluate);
    }

    /**
     * 批量删除用户评论
     *
     * @param ids 需要删除的用户评论主键
     * @return 结果
     */
    @Override
    public int deleteEvaluateByIds(Long[] ids)
    {
        return evaluateMapper.deleteEvaluateByIds(ids);
    }

    /**
     * 删除用户评论信息
     *
     * @param id 用户评论主键
     * @return 结果
     */
    @Override
    public int deleteEvaluateById(Long id)
    {
        return evaluateMapper.deleteEvaluateById(id);
    }


    /**
     *统计商品评论分类数量
     * @param goodsId 商品ID
     * @return 分类数量
     */
    @Override
    public EvaluationVo evaluateSum(Long goodsId){
        return evaluateMapper.evaluateSum(goodsId);
    }
}
